package com.administrator.customviewtest.view.floatCircleView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.administrator.customviewtest.R;

/**
 * Created by Administrator on 2016/8/8.
 */
public class FloatCircleView extends View {
    private Paint circlePaint;
    private Paint textPaint;
    private Rect textRect;

    public int width = 150;
    public int height = 150;
    String text = "50%";
    private boolean drag = false;
    private  Bitmap bitmap;



    public FloatCircleView(Context context) {
        this(context,null);
    }

    public FloatCircleView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FloatCircleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initPaint();
    }



    private void initPaint() {
        circlePaint = new Paint();
        circlePaint.setAntiAlias(true);
        circlePaint.setColor(Color.GRAY);

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setColor(Color.GREEN);
        textPaint.setTextSize(25);
        textRect = new Rect();
        textPaint.getTextBounds(text,0,text.length(),textRect);
        Bitmap src = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
         bitmap = Bitmap.createScaledBitmap(src,width,height,true);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if(drag){
            canvas.drawBitmap(bitmap,0,0,null);
            return;
        }

        canvas.drawCircle(width/2,height/2,width/2,circlePaint);
        canvas.drawText(text,0,text.length(),width/2-textRect.width()/2,height/2+textRect.height()/2,textPaint);
    }
    public void setDragState(boolean b){
        drag = b;
        invalidate();
    }
}
